package com.hy.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.hy.exception.GraceException;
import com.hy.grace.result.ResponseStatusEnum;
import com.hy.mapper.InterviewerMapper;
import com.hy.pojo.Interviewer;
import com.hy.pojo.bo.InterviewerBO;
import com.hy.service.InterviewerService;
import com.hy.service.JobService;
import com.hy.service.QuestionLibService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class InterviewerServiceImpl implements InterviewerService {


    @Autowired
    private InterviewerMapper interviewerMapper;

    @Autowired
    private JobService jobService;

    @Autowired
    private QuestionLibService questionLibService;

    /**
     * 创建或者更新面试官信息
     * @param interviewerBO
     */
    @Override
    public void createOrUpdate(InterviewerBO interviewerBO) {

        Interviewer interviewer = new Interviewer();
        BeanUtils.copyProperties(interviewerBO, interviewer);
        interviewer.setUpdatedTime(LocalDateTime.now());

        if (StringUtils.isBlank(interviewerBO.getId())) {
            // 创建
            interviewer.setCreateTime(LocalDateTime.now());
            interviewerMapper.insert(interviewer);
        } else {
            // 更新
            interviewer.setUpdatedTime(LocalDateTime.now());
            interviewerMapper.updateById(interviewer);
        }
    }

    /**
     * 查询所有面试官信息
     */
    @Override
    public List<Interviewer> queryAll() {
        // 根据更新时间倒序查询
        List<Interviewer> interviewerList = interviewerMapper.selectList(
                new QueryWrapper<Interviewer>().orderByDesc("updated_time"));

        return interviewerList;
    }

    @Override
    public void deleteById(String interviewerId) {
        // 删除面试官之前需要判断有没有职位和面试题库正在使用
        boolean jobFlag = jobService.isJobContainInterviewer(interviewerId);
        boolean questionFlag = questionLibService.isQuestionContainInterviewer(interviewerId);

        if (jobFlag || questionFlag) {
            GraceException.display(ResponseStatusEnum.CAN_NOT_DELETE_INTERVIEWER);
        }

        interviewerMapper.deleteById(interviewerId);
    }
}
